import numpy as np
from scipy import signal
import cv2
import matplotlib.pyplot as plt

# 视频参数
frame_width = 640
frame_height = 480
fps = 30
duration = 10  # 视频时长（秒）
total_frames = fps * duration

# 创建VideoWriter对象
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(r"E:\studylife\detectflaws\code\python\moNiImg\videos\compare2.mp4", fourcc, fps, (frame_width, frame_height))

# 生成背景部分方波变化的灰度值
bg_wave_period = 30  # 背景部分的周期（帧）
bg_wave_freq = 1 / bg_wave_period
#signal.square(2 * np.pi * bg_wave_freq * np.arange(total_frames))  这一段代码生成 -1  和  1 的方波，周期为1s
bg_gray_values = (signal.square(2 * np.pi * bg_wave_freq * np.arange(total_frames)) + 1) * 127
print(bg_gray_values)


# 生成“华师”区域方波变化的灰度值，延迟0.01秒
text_wave_period = 30  # “华师”区域的周期（帧）
text_wave_freq = 1 / text_wave_period
text_gray_values = (signal.square(2 * np.pi * bg_wave_freq * np.arange(total_frames) - 0.01 * 2 * np.pi) + 1) * 127
print(text_gray_values)

# 创建字母"Hua Shi"的mask
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 2
font_thickness = 5
text = "S C N U"
text_size = cv2.getTextSize(text, font, font_scale, font_thickness)[0]
text_x = (frame_width - text_size[0]) // 2
text_y = (frame_height + text_size[1]) // 2

for i in range(total_frames):
    # 创建一个新的帧
    frame = np.zeros((frame_height, frame_width, 3), np.uint8)

    # 背景部分填充
    #frame[:, :] = bg_gray_values[i]
    frame[:, :] = 100
    # for h in range(frame_height):
    #     if h < 250:
    #         frame[h, :] = bg_gray_values[i]
    #     else:
    #         frame[h, :] = (h / frame_height) * bg_gray_values[i]


    # “华师”区域填充
    # frame[text_y - text_size[1]:text_y, text_x:text_x + text_size[0]] = text_gray_values[i]


    # 写入字母"Hua Shi"
    if(text_gray_values[i] == 254):
        cv2.putText(frame, text, (text_x, text_y), font, font_scale, (255, 255, 255), font_thickness)
    else:
        cv2.putText(frame, text, (text_x, text_y), font, font_scale, (0, 0, 0), font_thickness)

    # 写入帧到视频
    out.write(frame)

print(np.shape(bg_gray_values))
print(bg_gray_values)
# 释放VideoWriter
out.release()